package com.joplus.housekeeper.ui.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.joplus.housekeeper.R;

/**
 * author： ccm
 * time： 2017/6/19.
 * description：
 */

public class SplashView extends View {

    private CountDownTimer countDownTimer;

    public SplashView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

    }

    public SplashView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    public SplashView(Context context) {
        super(context);
    }


    private int progress = 0;
    private int max = 100;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);


        Paint paint = new Paint();
        paint.setStrokeWidth(20);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(getResources().getColor(R.color.jo_main_yt));


        RectF rect = new RectF(0, 0, getWidth(), getHeight());

        if (progress != 0) {
            canvas.drawArc(rect, 360 * progress / max - 90, 360 * (100 - progress) / max, true, paint);
        }


    }

    public void cancel() {
        countDownTimer.onFinish();
    }

    public void start() {
        setVisibility(VISIBLE);
        progress = 0;
        countDownTimer = new CountDownTimer(500, 25) {
            @Override
            public void onTick(long l) {
                progress += 5;
                invalidate();
            }

            @Override
            public void onFinish() {
                setVisibility(GONE);
            }
        };

        countDownTimer.start();

    }


}
